﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.ComponentModel;
using FinPlusAssembler;
using FinPlusInterfaces;
using FinPlusCompCore;

namespace FinPlusCompProxy
{
    public class QLEngineBondCurves : FinPlusComponent
    {
        [State(Test = true)]
        public override bool IsValid { get; set; }

        private enum Params { CurveName, LinkedCurve };
        private readonly IFinPlusComp _bondCurveBuildConn, _instrumentBuildConn, _instrumentConn;
        private readonly QLAnalytics _analytics;
        private readonly BondCurves _bondCurves;
        private readonly BuildEvent _buildEvents;
   
        //construct
        public QLEngineBondCurves(IFinPlusComp analytics, IFinPlusComp bondCurveBuilds, IFinPlusComp instrumentBuilds, IFinPlusComp instruments, BuildEvent buildEvents)
        {
            _analytics = (QLAnalytics)analytics;
            _bondCurveBuildConn = bondCurveBuilds;
            _instrumentBuildConn = instrumentBuilds;
            _instrumentConn = instruments;
            _buildEvents = buildEvents;
            _bondCurves = new BondCurves();

            foreach (var bondCurveBuild in _bondCurveBuildConn.Adapter.Values)
            {
                var curveName = bondCurveBuild[Params.CurveName.ToString()].ToString();
                _bondCurves.TryAdd(curveName, new BondCurve(_analytics, curveName, bondCurveBuild, _instrumentBuildConn.Adapter,
                     _instrumentConn.Adapter.ToList(Params.LinkedCurve.ToString(), curveName, ','), _buildEvents));
            }

            IsValid = true;
        }

        //common control interface
        public override void Dispose()
        {
            if(_instrumentConn != null)
                _instrumentConn.Dispose(); 

            if(_bondCurveBuildConn != null)
                _bondCurveBuildConn.Dispose();

            if (_instrumentBuildConn != null)
                _instrumentBuildConn.Dispose(); 
        }
      }
}
